#include <unistd.h>
#include <stdio.h>
#include <string.h>>
#include "socklib.h"

#define LEN 1024
#define HOST "10.175.69.50"

void talk_with_server(int fd);

int main()
{
    int sock_id = connect_to_server(HOST,1496);
    if (sock_id == -1){
        return -1;
    }

	//printf("sock_id = %d\n",sock_id);

    talk_with_server(sock_id);

    close(sock_id);

    return 0;
}

void talk_with_server(int fd)
{
    char buf[LEN];
    int n;

	while(1){
		char c;
		int index;

		index = 0;
		c = getchar();
		while(c != 10){
			buf[index++] = c;
			c = getchar();
		}
		buf[index] = '\0';
		if (strcmp(buf,"bye") == 0){
			printf("Bye...\n");
			break;
		}

		printf("Send data to server...\n");
		n = write(fd,buf,strlen(buf));
		if (n < 0){
			perror("sock write error");
			break;
		}

		printf("Recv data from server...\n");
		n = read(fd,buf,LEN);
		if (n){
			write(1,buf,n);
			write(1,"\n",1);
		}
		sleep(1);
	}
}


